Copyright: (c) James L. Grant 1998
Citation: PrintPreview Help V1.0


PrintPreview Control

The PrintPreview ActiveX control allows Visual Basic programmers to direct output to a preview screen or to the printer using the same instructions.  Output to PrintPreview can be sent to a preview screen or directly to the printer from the user's code.  Also, any page of the preview output, or the entire preview document, can be printed from the preview screen at the user's discretion.


How to use the PrintPreview Control

To use PrintPreview, create a form and place an instance of PrintPreview on the form.  Be sure that the form and the PrintPreview instance are large enough to show all of the control.

Use the default name of the PrintPreview instance, or change the default name to one more meaningful.  Text and graphics are displayed on the Preview screen or printed directly by setting various properties of the control and calling various methods.


Using Properties

Properties set remain in effect until specifically changed.  The only exceptions to this rule are the CurrentX and CurrentY properties, which usually are changed when display methods such as PreviewLine, PreviewCircle, or PreviewText are executed.

Properties, such as PaperSize, Orientation, and similar commands that establish printer configurationsPrinterSetupCommands must be entered at the beginning of a page, before any output is sent to the page.  Properties changed improperly usually are identified as PrintPreview processes the instructions; however, PrintPreview may not account for idiosyncrasies of all printers.


Output

Output is fashioned in a way analogous to Visual Basic printing.  Output is directed to the Preview screen or the Printer through the Output property.  When Output is set to prvOutputScreen (=1), the output is displayed on the Preview screen. When Output is set to prvOutputPrinter (=2), output is sent directly to the printer.

Output to the Preview screen may be sent to the printer by the user.  Either the entire document or individual pages can be printed in this way.


Printing

Documents may be printed through PrintPreview directly, without any preview display.  In addition, documents displayed on the Preview screen may be printed directly from the screen.  Either the entire document or individual pages can be printed in this way.


How PrintPreview Works

This control works by writing files for individual pages of the document.  These files contain all the print commands the user has issued.  When a page is displayed or printed, the file is read, and each command is executed in sequence either on the Preview screen or the Printer.  The temporary files are erased when the control is exited normally.


Using the PrintPreview Screen

The preview screen allows the user to scroll through the entire document, one page at a time.  The user should keep in mind that formatting commands issued remain in effect until expressly overridden.  This generally does not cause a problem when the document is scrolled in the forward order, but may cause unexpected results when the document is scrolled in reverse.  Basic formatting commands are repeated at the beginning of each page to minimize such problems; however, some combinations of commands may cause formatting differences when a page is accessed in forward or backward directions.

In some cases, the unexpected results will be transmitted to printed copies when copies of a single page are printed from the preview screen after the previewed document has been scrolled in the reverse direction.  These results can be avoided by considering the effects of backward scrolling when the output is generated, or by returning the document to the beginning after reverse scrolling and before printing.


Example Code

The following code fragment creates a two-page document on a PrintPreview control named Preview1.  To execute this example code, create a Visual Basic project, and add the PrintPreview control.  On Form1, draw the PrintPreview control, and name it Preview1.  Place the following code in the Activate section of the form (the purpose of each line of code is explained by the accompanying comment).

Dim longstrings As String     ' this is a variable to hold a long line of text

longstrings = "Now is the time for all good men to come to the aid of their country.  The quick brown fox jumped over the lazy dog.  The rain in spain falls mainly on the plain.  The quick brown fox jumped over the lazy dog."

Preview1.Output = prvOutputScreen    '(=1)  Directs output to preview screen.

Preview1.PaperSize = 1    ' sets the paper size and the preview screen to 8.5" x 11".

Preview1.Orientation = 2    ' sets the display orientation to landscape

Preview1.FontName = "Arial"   'sets the font to Arial

Preview1.FontSize = 12    'sets the font size to 12 points

Preview1.FontStrikethru = True      ' sets fontstrikethrough to true

Preview1.CurrentX = 1000

Preview1.CurrentY = 1000      ' sets the current position to 1000,1000 twips

Preview1.PreviewText longstrings, False      ' prints the long line of text.  Does not advance to the next line after printing.

Preview1.FontStrikethru = False     ' turns off strikethrough

Preview1.CurrentX = 300

Preview1.CurrentY = 3000       ' sets x,y to 300,3000 twips

Preview1.FontSize = 16     ' sets font size to 16 points

Preview1.FontUnderline = True     ' turns on font underline

Preview1.FontName = "Times New Roman"     ' sets font to TimesNewRoman

Preview1.FontBold = True        ' turns on font bold

Preview1.FontItalic = True       ' turns on font italic

Preview1.PreviewText "Some more text to look at", False       ' prints text, does not advance to the next line.

Preview1.PreviewCircle 2000, 2000, 1500       ' draws a circle with radius 1500 twips and center at 2000,2000 twips.

Preview1.PreviewLine 1000, 2000, 3000, 2200, QBColor(7), "BF"      ' draws a box from 1000,2000 to 3000,2200, fills the box with QB color 7.

Preview1.PreviewPSet 1000, 1500, 3, QBColor(4)      ' draws a point at 1000,1500.  Three pixels square and QB color 4 (red)

Preview1.NewPage        ' advances to the next page

Preview1.FontName = "Arial"     ' changes font to Arial

Preview1.FontSize = 12     ' sets font size to 12 points

Preview1.FontStrikethru = True     ' turns on strikethrough

Preview1.CurrentX = 1000

Preview1.CurrentY = 1000      ' sets current x,y

Preview1.PreviewText "This is page 2 of the test", False        ' prints a line of text.  Does not advance to the next line.

Preview1.FontStrikethru = False     ' turns off strikethrough

Preview1.CurrentX = 300

Preview1.CurrentY = 3000     ' resets current x,y

Preview1.FontSize = 16     ' sets font size to 16 points.

Preview1.FontUnderline = True     ' turns on underline

Preview1.FontName = "Times New Roman"     ' changes font

Preview1.FontBold = True     ' turns on bold

Preview1.FontItalic = True     ' turns on italic

Preview1.PreviewText "Some more text to look at on page 2", False      ' prints another line of text with above attributes.

Preview1.PreviewCircle 2000, 2000, 1500, QBColor(4)       ' draws a red circle with radius 1500 twips and center at 2000,2000 twips

Preview1.PreviewLine 1000, 2000, 3000, 2400, QBColor(4), "B"       ' draws a red box from 1000,2000 to 3000,2400. Does not fill box.

Preview1.PreviewPSet 1000, 1500, 3, QBColor(4)     ' draws a red point

Preview1.FontBold = False     ' turns off bold

Preview1.FontItalic = False     ' turns off italic

Preview1.FontUnderline = False         ' turns off font underline.  These commands are needed because Bold, Italic, Underlining otherwise would remain on if previous pages were displayed after this page.

Preview1.EndDocument      ' causes the document to be displayed.

(end of code fragment)


Error Handling

PrintPreview handles errors in three different ways, depending upon the cause and timing of the error.  Errors encountered in references to properties or methods are raised directly through the Err object.  The information returned includes the error number, a brief description of the error, and the property or method that generated the error.

Errors encountered in response to direct user action, such as clicking on the "Next Page" button or the "Print Document" button, are indicated by a message box informing the user of the error, and indicating the cause of the error.

Internal errors are indicated by the firing of the PreviewError event.  In the PreviewError event, the user can determine the type, cause, and source of the error by examining the read-only properties ErrorNumber, ErrorDescription, and ErrorSource.  Internal errors occur when some unanticipated condition occurs within the control, or when there is a hardware malfunction occurs on the system on which the control is executing.


PrintPreview Events

PreviewError

This event is raised when an internal error occurs within the control.  These errors are generated within routines that do not interface directly with the user.  Ordinarily, such errors indicate bugs in the control programming, or malfunction of the computer system on which the control is executing.

The control returns information about the cause of the internal error in the read-only properties ErrorNumber, ErrorDescription, and ErrorSource.  This information should be supplied if available when reporting bugs.


PrintPreview Properties & Methods

The following is a list of properties and methods supported by PrintPreview:

ColorMode (Integer) - Property

PrintQuality (Integer) - Property

Copies (Integer) - Property

BackColor (OLE_Color) - Property

ForeColor (OLE_Color) - Property

CurrentPage (Long) - Property (Read only)

CurrentX (Single) - Property

CurrentY (Single) - Property

DrawMode (Integer) - Property

DrawStyle (Integer) - Property

DrawWidth (Integer) - Property

EndDocument - (Method)

FillColor (OLE_COLOR) - Property

FillStyle (Integer) - Property

Font (Font) - Property

FontBold (Boolean) - Property

FontItalic (Boolean) - Property

FontName (String) - Property

FontSize (Single) - Property

FontStrikethru (Boolean) - Property

FontTransparent (Boolean) - Property

FontUnderline (Boolean) - Property

ForeColor (OLE_COLOR) - Property

LastPage (Long) - Read-Only Property

NewPage - (Method)

NewSection - (Method)

Orientation (Integer) - Property

Output (Integer) - Property

Page (Long) - Read-Only Property

PageHeight (Single) - Property

PageWidth (Single) - Property

PaperBin (Integer) - Property

PaperSize (Integer) - Property

PreviewCircle (Method)

PreviewLine (Method)

PreviewPSet (Method)

PreviewText (Method)

PreviewTextHeight (Single) - Read-Only Property

PreviewTextWidth (Single) - Read-Only Property

Resize (Method)

TwipsPerPixelX (Single)

TwipsPerPixelY (Single)


ColorMode (Integer) - Property

Determines whether printed output is in monochrome or in color.  This property cannot be specified after any output is written to the current page.

Allowable values are:

vbPRCMMonochrome = 1   'Monochrome output

vbPRCMColor = 2        'Color output

This property is meaningless unless Output is to the printer.

If the printer does not support color printing, this property is ignored.

See also:	Printer Setup Commands


Copies (Integer) - Property

Determines the number of copies printed.

This property is meaningless unless Output is to the printer.

This property cannot be specified after any output is written to the current page.

See Also:	Printer Setup Commands


BackColor (OLE_Color) - Property

This property cannot be specified after any output is written to the current page.

Returns or sets the backcolor for the preview background.  This property must be specified as a RGB value.


ForeColor (OLE_Color) - Property

Returns or sets the forecolor for the Preview screen or the Printer.  This property must be specified as a RGB value.

RGB or QBColor functions can be used to set this value


FillColor (OLE_Color) - Property

Returns or sets the fillcolor for the Preview screen or the printer.  This property must be specified as a RGB value.

RGB or QBColor functions can be used to set this value


CurrentPage (Long) - Property (Read only)

Returns the current page of the document


CurrentX ,CurrentY (Single) - Property

Sets or returns the x-value (y-value) at which the next character will be printed. (Value is in Twips)


DrawMode (Integer) - Property

Sets or returns the current DrawMode of the preview screen or the printer.


DrawStyle (Integer) - Property

Sets or returns the current DrawStyle of the preview screen or the printer.


DrawWidth (Integer) - Property

Sets or returns the current DrawWidth of the preview screen or the printer.


EndDocument - (Method)

Informs the OCX to stop receiving input and display or print the first page of the document.


FillStyle (Integer) - Property

Sets or returns the current FillStyle of the preview screen or the printer.

 printer.


Font (Font) - Property

Sets or returns the current font of the preview screen or the printer.


Individual Font Properties

FontBold (Boolean) - Property

FontItalic (Boolean) - Property

FontName (String) - Property

FontSize (Single) - Property

FontStrikethru (Boolean) - Property

FontTransparent (Boolean) - Property

FontUnderline (Boolean) - Property

These properties set or return individual font properties for the preview screen or the printer.


LastPage (Long) - Read-Only Property

Returns the last page of the document.


NewPage - (Method)

Informs PrintPreview to begin a new page.  This command should be used to begin output on a different page whenever the printer format of the following page will not be changed.

See Also:	NewSection


NewSection - (Method)

Informs PrintPreview to begin a new output section.  This command should be used instead of NewPage whenever the layout of the following pages will be different (for example, a different page orientation).

See Also:	NewPage

Printer Setup Commands


Orientation (Integer) - Property

Sets or returns the orientation of the preview screen or the printer.

Values are:	vbPRORPortrait = 1   'Documents print with the top at the narrow side of the paper

vbPRORLandscape = 2  'Documents print with the top at the wide side of the paper

This property cannot be specified after any output is written to the current page.

See also:	Printer Setup Commands


Output (Integer) - Property

Sets or returns the output device.  Output initially directed to the screen can be sent to the printer directly from the OCX.

Values are:

prvOutputScreen = 1 ' to screen

prvOutputPrinter = 2 'to printer

This property cannot be specified after any output is written to the current page.

See also:	Printer Setup Commands


Page (Long) - Read-Only Property

Returns the number of the current page.  Not available at design time.


Page Dimensions

PageHeight (Single) - Property

PageWidth (Single) - Property

Sets or returns the page height or page width of the preview screen or the printer.  Values are in inches.  To set these properties, the paper size must be set to 256. (See Paper Size)

These properties cannot be specified after any output is written to the current page.

See also:	Printer Setup Commands


PaperBin (Integer) - Property

Sets or returns the paper bin from which the printer will obtain paper for subsequent pages.

Values are

vbPRBNUpper = 1  'Use paper from the upper bin.

VbPRBNLower = 2  'Use paper from the lower bin.

VbPRBNMiddle = 3     'Use paper from the middle bin.

VbPRBNManual = 4     'Wait for manual insertion of each sheet of paper.

VbPRBNEnvelope = 5   'Use envelopes from the envelope feeder.

VbPRBNEnvManual = 6  'Use envelopes from the envelope feeder, but wait for manual insertion.

VbPRBNAuto = 7   '(Default) Use paper from the current default bin.

VbPRBNTractor = 8    'Use paper fed from the tractor feeder.

VbPRBNSmallFmt = 9   'Use paper from the small paper feeder.

VbPRBNLargeFmt = 10  'Use paper from the large paper bin.

VbPRBNLargeCapacity = 11 'Use paper from the large capacity feeder.

VbPRBNCassette = 14  'Use paper from the attached cassette cartridge.

This property cannot be specified after any output is written to the current page.

See also:	Printer Setup Commands


PaperSize (Integer) - Property

Sets or returns the coded size of the preview screen or the printer.

Valid values are:

vbPRPSLetter = 1    ' Letter, 8 1/2 x 11 in.

vbPRPSLetterSmall = 2   ' Letter Small, 8 1/2 x 11 in.

vbPRPSTabloid = 3   ' Tabloid, 11 x 17 in.

vbPRPSLedger = 4    ' Ledger, 17 x 11 in.

vbPRPSLegal = 5  'Legal, 8 1/2 x 14 in.

vbPRPSStatement = 6  'Statement, 5 1/2 x 8 1/2 in.

vbPRPSExecutive = 7  'Executive, 7 1/2 x 10 1/2 in.

vbPRPSA3 = 8     'A3, 297 x 420 mm

vbPRPSA4 = 9    ' A4, 210 x 297 mm

vbPRPSA4Small = 10   'A4 Small, 210 x 297 mm

vbPRPSA5 = 11    'A5, 148 x 210 mm

vbPRPSB4 = 12    'B4, 250 x 354 mm

vbPRPSB5 = 13    'B5, 182 x 257 mm

vbPRPSFolio = 14 'Folio, 8 1/2 x 13 in.

vbPRPSQuarto = 15    'Quarto, 215 x 275 mm

vbPRPS10x14 = 16 '10 x 14 in.

vbPRPS11x17 = 17 '11 x 17 in.

vbPRPSNote = 18  'Note, 8 1/2 x 11 in.

vbPRPSEnv9 = 19  'Envelope #9, 3 7/8 x 8 7/8 in.

vbPRPSEnv10 = 20 'Envelope #10, 4 1/8 x 9 1/2 in.

vbPRPSEnv11 = 21 'Envelope #11, 4 1/2 x 10 3/8 in.

vbPRPSEnv12 = 22 'Envelope #12, 4 1/2 x 11 in.

vbPRPSEnv14 = 23 'Envelope #14, 5 x 11 1/2 in.

vbPRPSCSheet = 24    'C size sheet

vbPRPSDSheet = 25    'D size sheet

vbPRPSESheet = 26    'E size sheet

vbPRPSEnvDL = 27 'Envelope DL, 110 x 220 mm

vbPRPSEnvC3 = 29 'Envelope C3, 324 x 458 mm

vbPRPSEnvC4 = 30 'Envelope C4, 229 x 324 mm

vbPRPSEnvC5 = 28 'Envelope C5, 162 x 229 mm

vbPRPSEnvC6 = 31 'Envelope C6, 114 x 162 mm

vbPRPSEnvC65 = 32    'Envelope C65, 114 x 229 mm

vbPRPSEnvB4 = 33 'Envelope B4, 250 x 353 mm

vbPRPSEnvB5 = 34 'Envelope B5, 176 x 250 mm

vbPRPSEnvB6 = 35 'Envelope B6, 176 x 125 mm

vbPRPSEnvItaly = 36  'Envelope, 110 x 230 mm

vbPRPSEnvMonarch = 37    'Envelope Monarch, 3 7/8 x 7 1/2 in.

vbPRPSEnvPersonal = 38   'Envelope, 3 5/8 x 6 1/2 in.

vbPRPSFanfoldUS = 39 'U.S. Standard Fanfold, 14 7/8 x 11 in.

vbPRPSFanfoldStdGerman = 40  'German Standard Fanfold, 8 1/2 x 12 in.

vbPRPSFanfoldLglGerman = 41  'German Legal Fanfold, 8 1/2 x 13 in.

vbPRPSUser = 256 'User-defined

This property cannot be specified after any output is written to the current page.

See also:	Printer Setup Commands


PreviewCircle (Method)

Displays or prints a circle or ellipse.  Identical to the VB Circle method


PreviewLine (Method)

Displays or prints a line or rectangle.  Identical to the VB Line method, except that the B and BF parameters in PreviewLine are characters.


PreviewPSet (Method)

Displays or prints a point.  Identical to the VB PSet method


PreviewText (Method)

Displays or prints a line of text.

Example:

object.PreviewText Textline,nl

where 	object is a PrintPreview instance

Textline is a line of text

nl is a boolean variable that is true if the next text begins on a new line, false if it begins on the same line (similar to the VB ';' flag)


PrintQuality (Integer) - Property

Determines the quality of printed output

Allowable values are:

vbPRPQDraft = -1 'Draft resolution

vbPRPQLow = -2   'Low resolution

vbPRPQMedium = -3    'Medium resolution

vbPRPQHigh = -4  'High resolution

This property is meaningless unless Output is to the printer.

This property cannot be specified after any output is written to the current page.

See also:	Printer Setup Commands


Text Properties

PreviewTextHeight (Single) - Read-Only Property

PreviewTextWidth (Single) - Read-Only Property

These properties return the height and width of a block of text.  The values returned are in twips.  If the text block contains multiple lines, the width returned is for the longest line, and the height is for the entire block.

Values returned include leading and spacing.


Resize (Method)

Scales the PrintPreview control to the size of the form containing it.


TwipsPerPixel

TwipsPerPixelX (Single)

TwipsPerPixelY (Single)

Returns the number of twips represented by a single pixel on the preview screen or the printer.


Printer Setup Commands

The Printer Setup Commands are:

Paperbin

PaperSize

Orientation

PrintQuality

ColorMode

Copies